import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import { getToken, getRoleId } from '@/utils/auth'

const baseURL = process.env.BASE_API

// const service = async ({ url, method, data }) => {
//   let res = new Promise((res, err) => res({ data: {} }));
//   try {
//     switch (method) {
//       case "get":
//         res = api.get(url);
//         break;
//       case "post":
//         res = api.post(url, data)
//         break;
//       case "put":
//         res = api.put(url, data)
//         break;
//       case "delete":
//         res = api.del(url, data)
//         break;
//     }
//     if ((url.endsWith('admin/list') || url.endsWith('/actionlog/list')) && getRoleId() !== '1') {
//       let url = window.location.href.split('#')[0] + '#/race-manage'
//       localStorage.setItem('active', 5)
//       window.location.href = url
//     }
//     return res;
//   } catch (e) {
//     Message({
//       message: '网站正在维护，请稍后访问',
//       type: 'error',
//       duration: 5 * 1000
//     })
//     return Promise.reject(e)
//   }
// }

// create an axios instance
const service = axios.create({
  baseURL, // api的base_url
  timeout: 30000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    console.log(getToken())
    config.headers['Content-Type'] = 'application/json;charset=UTF-8'
    config.headers['User-Token'] = getToken()
    return config
  },
  error => {
    console.log(error) // for debug
    // Promise.reject(error)
  }
)
// respone interceptor
service.interceptors.response.use(
  response => {
    const res = response.data

    const requestUrl = response.request.responseURL
    if ((requestUrl.endsWith('admin/list') || requestUrl.endsWith('/actionlog/list')) && getRoleId() !== '1') {
      let url = window.location.href.split('#')[0] + '#/race-manage'
      localStorage.setItem('active', 5)
      window.location.href = url
      return response
    }

    if (res.result !== 0 &&
      response.request.responseURL !== 'http://dms20180816.oss-cn-hangzhou.aliyuncs.com/' &&
      response.request.responseURL !== 'https://sundax2.oss-cn-hongkong.aliyuncs.com/' &&
      response.request.responseURL.indexOf('api/v1/') === -1 &&
      response.request.responseURL.indexOf('aliyuncs') === -1) {
      let message = res.message.zh
      if (res.message.ext !== null && res.message.ext !== undefined && res.message.ext !== '') {
        message = message + '，' + res.message.ext
      }
      if (store.getters.language === 'en') {
        message = res.message.en
      }
      Message({
        message: message,
        type: 'error',
        duration: 5 * 1000
      })
      if (res.result === -101004 || res.result === -101005 || res.result === -101001 || res.result === -101002 || res.result === -101003) {
        store.dispatch('OuttimeLogOut', null).then(() => {
          // this.$root.eventBus.$emit('exit')
        })
        let url = window.location.href.split('#')[0] + '#/login'
        window.location.href = url
      }
      //  return response
    } else {
      return response
    }
  },
  error => {
    Message({
      message: '网站正在维护，请稍后访问',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
